package com.intelligent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.intelligent.mapper.CustomerManagementMapper;
import com.intelligent.mapper.PaymentOrderMapper;
import com.intelligent.mapper.TUserMapper;
import com.intelligent.pojo.CustomerManagement;
import com.intelligent.pojo.PaymentOrder;
import com.intelligent.pojo.TUser;
import com.intelligent.service.PaymentOrderService;
import com.intelligent.vo.PaymentOrderCreateReq;
import com.intelligent.vo.PaymentOrderPageReq;
import com.intelligent.vo.PaymentOrderResp;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.stream.Collectors;

@Service
public class PaymentOrderServiceImpl extends ServiceImpl<PaymentOrderMapper, PaymentOrder>
    implements PaymentOrderService{

    @Resource
    private CustomerManagementMapper customerManagementMapper;

    @Resource
    private TUserMapper tUserMapper;

    @Override
    public Page<PaymentOrderResp> page(PaymentOrderPageReq req) {
        Page<PaymentOrder> entityPage = new Page<>(req.getPageNum(), req.getPageSize());
        LambdaQueryWrapper<PaymentOrder> qw = new LambdaQueryWrapper<>();

        if (req.getPaymentOrderNo() != null && !req.getPaymentOrderNo().trim().isEmpty()) {
            qw.like(PaymentOrder::getPaymentOrderNo, req.getPaymentOrderNo().trim());
        }
        if (req.getPaymentStatus() != null) {
            qw.eq(PaymentOrder::getPaymentStatus, req.getPaymentStatus());
        }
        if (req.getPaymentType() != null && !req.getPaymentType().trim().isEmpty()) {
            qw.eq(PaymentOrder::getPaymentType, req.getPaymentType().trim());
        }
        if (req.getCustomerId() != null) {
            qw.eq(PaymentOrder::getCustomerId, req.getCustomerId());
        }
        if (req.getRelatedSalesOrder() != null && !req.getRelatedSalesOrder().trim().isEmpty()) {
            qw.like(PaymentOrder::getRelatedSalesOrder, req.getRelatedSalesOrder().trim());
        }

        qw.orderByDesc(PaymentOrder::getId);
        this.page(entityPage, qw);

        Page<PaymentOrderResp> voPage = new Page<>(entityPage.getCurrent(), entityPage.getSize(), entityPage.getTotal());
        voPage.setRecords(entityPage.getRecords().stream().map(this::toResp).collect(Collectors.toList()));
        return voPage;
    }

    @Override
    public PaymentOrderResp detail(Long id) {
        PaymentOrder paymentOrder = this.getById(id);
        return paymentOrder == null ? null : toResp(paymentOrder);
    }

    @Override
    public PaymentOrderResp create(PaymentOrderCreateReq req) {
        PaymentOrder paymentOrder = new PaymentOrder();
        paymentOrder.setPaymentOrderNo(req.getPaymentOrderNo());
        paymentOrder.setPaymentStatus(req.getPaymentStatus());
        paymentOrder.setPaymentType(req.getPaymentType());
        paymentOrder.setCustomerId(req.getCustomerId());
        paymentOrder.setRelatedSalesOrder(req.getRelatedSalesOrder());
        paymentOrder.setPayableAmount(req.getPayableAmount());
        paymentOrder.setWrittenOffAmount(req.getWrittenOffAmount());
        paymentOrder.setUId(req.getUId());
        paymentOrder.setCreationDate(req.getCreationDate());

        // 如果付款单号为空，自动生成
        if (paymentOrder.getPaymentOrderNo() == null || paymentOrder.getPaymentOrderNo().trim().isEmpty()) {
            paymentOrder.setPaymentOrderNo("FK" + System.currentTimeMillis());
        }

        // 如果创建日期为空，设置为当前时间
        if (paymentOrder.getCreationDate() == null) {
            paymentOrder.setCreationDate(new Date());
        }

        // 如果已核销金额为空，设置为0
        if (paymentOrder.getWrittenOffAmount() == null) {
            paymentOrder.setWrittenOffAmount(BigDecimal.ZERO);
        }

        this.save(paymentOrder);
        return toResp(paymentOrder);
    }

    private PaymentOrderResp toResp(PaymentOrder p) {
        PaymentOrderResp r = new PaymentOrderResp();
        r.setId(p.getId());
        r.setPaymentOrderNo(p.getPaymentOrderNo());
        r.setPaymentStatus(p.getPaymentStatus());


        // 计算待核销金额
        BigDecimal payableAmount = p.getPayableAmount() != null ? p.getPayableAmount() : BigDecimal.ZERO;
        BigDecimal writtenOffAmount = p.getWrittenOffAmount() != null ? p.getWrittenOffAmount() : BigDecimal.ZERO;
        r.setToBeWrittenOffAmount(payableAmount.subtract(writtenOffAmount));

        // 获取客户名称
        if (p.getCustomerId() != null) {
            CustomerManagement cm = customerManagementMapper.selectById(p.getCustomerId());
            if (cm != null) {
                r.setCustomerName(cm.getCustomerName());
            }
        }

        // 获取财务人员姓名
        if (p.getUId() != null) {
            TUser user = tUserMapper.selectById(p.getUId());
            if (user != null) {
                r.setFinanceUserName(user.getRealName() != null ? user.getRealName() : user.getUsername());
            }
        }

        return r;
    }
}




